package com.leijingqi.service.impl;

import com.leijingqi.mapper.DeptMapper;
import com.leijingqi.mapper.EmpMapper;
import com.leijingqi.pojo.DeptLog;
import com.leijingqi.pojo.Dept;
import com.leijingqi.service.DeptLogService;
import com.leijingqi.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private DeptLogService deptLogService;

    @Override
    public List<Dept> list() {
        return deptMapper.list();
    }
    @Transactional(rollbackFor = Exception.class)   //开启事务  spring事务管理   //Exception.class所有问题都回滚
    @Override
    public void delete(Integer id) throws Exception {
        try {
            deptMapper.deleteById(id);

            int i = 1/0;
//        if(true){
//            throw new Exception("报错拉...");
//        }

            empMapper.deleteByDeptId(id);
        } finally {
            //        //记录日志
            DeptLog deptlog = new DeptLog();
            deptlog.setCreateTime(LocalDateTime.now());
            deptlog.setDescription("执行了解散部门的操作,"+id+"号部门");
            deptLogService.insert(deptlog);
        }
    }
    @Override
    public void add(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.inset(dept);
    }

    @Override
    public Dept getData(Integer id) {
       return deptMapper.select(id);
    }

    @Override
    public void update(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.update(dept);
    }
}
